package com.wtpaint.linkedlist;

public class LinkedList<E> {

    //结点类
    private class Node {

        //当前元素
        public E e;
        //链表指向的下一个元素  //如果是双向链表，就得要添加一个指向的上一个元素了
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return super.toString();
        }
    }

    //虚拟头结点
    private Node dummyHead;

    public LinkedList() {
        dummyHead = new Node();
        size = 0;
    }

    private int size;

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(int index, E e) {
        judge(index);
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    public void addFirst(E e) {
        add(0, e);
    }

    public void addLast(E e) {
        add(size, e);
    }

    public E get(int index) {
        judge(index);

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    public void set(int index, E e) {
        judge(index);

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while (null != cur) {
            if (cur.e.equals(e)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public E remove(int index) {
        /* 删除的基本思路：将当前结点的下一个结点付给当前结点，然后将当前结点的下一个结点设置为空**/
        judge(index);
        Node pre = dummyHead;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        Node delNode = pre.next;
        pre.next = delNode.next;
        delNode.next = null;
        size--;
        return delNode.e;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public void removeElement(E e) {
        //1找到要删除的结点
        Node pre = dummyHead;
        while (null != pre.next) {
            if (pre.next.e.equals(e)) {
                break;
            }
            pre = pre.next;
        }

        //2删除当前结点
        if (null != pre.next) {
            Node delNode = pre.next;
            pre.next = delNode.next;
            delNode.next = null;
            size--;
        }
    }

    private void judge(int index) {
        //对index边界检查在数据结构中尤为重要
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("add Failed");
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node cur = dummyHead.next;
        while (null != cur) {
            sb.append(cur).append("->");
            cur = cur.next;
        }
        sb.append("NULL");
        return sb.toString();
    }
}
